<?php
/**
 * Created by PhpStorm.
 * User: 张福来
 * Date: 2018/10/29
 * Time: 14:52
 */

namespace app\api\model;
use Think\Db;
use think\Model;
class User extends Model{
    protected function initialize()
    {
        //需要调用`Model`的`initialize`方法
        parent::initialize();
        //TODO:自定义的初始化
    }
    protected function getCtimeAttr($vlue){
        return date("Y-m-d H:i:s",$vlue);
    }
    protected function getLastLoginTimeAttr($vlue){
        return date("Y-m-d H:i:s",$vlue);
    }
    protected function getUtimeAttr($vlue){
        return date("Y-m-d H:i:s",$vlue);
    }
    public function selecUser($pageNum,$listNum,$w){
        $reslt["list"] = $this
            ->where($w)
            ->page($pageNum,$listNum)
            ->order("ctime desc")
            ->select();
        $reslt["total"] = $this
                        ->where($w)
                        ->count();
        return $reslt;
    }
    /*
    * Notes: 检查用户登录
    * Create Date: 2018/10/29
    * Create Time: 16:53
    * Create by :张福来
    * $w : 条件
    */
    public function checkUserLogin($w){
        $result = $this
                 ->where($w)
                 ->find();
        return $result;
    }
    /*
    * Notes: 更新用户数据
    * Create Date: 2018/10/29
    * Create Time: 16:53
    * Create by :张福来
    * $con : 条件
    * $save : 更新数据
    */
    public function updateUser($save,$con){
        $result = $this
                ->where($con)
                ->update($save);
        return $result;
    }
    /*
    * Notes: 记录用户信息
    * Create Date: 2018/10/29
    * Create Time: 16:53
    * Create by :张福来
    * $data : 记录数据
    */
    public function addUser($data){
        $result = $this
                ->insert($data);
        $u_id = $this->getLastInsID();
        return $u_id;
    }

    /*
    * Notes: 积分操作方法
    * Create Date: 2018/10/5
    * Create Time: 下午3:28
    * Create by :张福来
    * $addorless : 1 加积分 2 减积分
    * $lessunm : 判断减积分时的积分余额
    */
    public function walletoperate($u_id,$addorless,$intergral,$operate){
        $w["u_id"] = $u_id;
        $uintergral = $this
                     ->field("wallet")
                     ->where($w)
                     ->find();
        if($addorless == 1){
            $addarray["u_id"] = $u_id;
            $addarray["from_num"] = $intergral;
            $addarray["go_num"] = 0;
            $addarray["message"] = $operate;
            $addarray["type"] = $addorless;
            $addarray["ctime"] = time();
            Db::startTrans();
            $result = $this
                      ->where($w)
                      ->setInc("wallet",$intergral);
            $result2 =Db::name("bill")
                     ->insert($addarray);
            if($result && $result2){
                Db::commit();
                return 200;
            }else{
                Db::rollback();
                return 201;
            }
        }elseif($addorless == 2){
            $addarray["u_id"] = $u_id;
            $addarray["from_num"] = 0;
            $addarray["go_num"] = $intergral;
            $addarray["message"] = $operate;
            $addarray["type"] = $addorless;
            $addarray["ctime"] = time();
            $lessunm = $uintergral['wallet']-$intergral;
            if($lessunm < 0){
                return 202;
            }else{
                Db::startTrans();
                $result = $this
                    ->where($w)
                    ->setDec("wallet",$intergral);
                $result2 =Db::name("bill")
                    ->insert($addarray);
                if($result && $result2){
                    Db::commit();
                    return 200;
                }else{
                    Db::rollback();
                    return 201;
                }
            }
        }
    }
    public function eidtUser($addarray){
        $u_id = $addarray['u_id'];
        unset($addarray['u_id']);
        $result = $this
                ->where(["u_id"=>$u_id])
                ->update($addarray);
        return $result;
    }
    public function userSign($addarray){
        $result = Db::name("sign")
                ->insert($addarray);
        return $result;
    }
    public function walletlist($pageNum,$listNum,$w){
        $result["list"] = Db::name("userforbill")
            ->where($w)
            ->page($pageNum,$listNum)
            ->order("ctime desc")
            ->select();
        $result["total"] = Db::name("userforbill")
            ->where($w)
            ->count("u_id");
        return $result;
    }
    public function changePhone($w,$c){
        $result = $this
                ->where($w)
                ->update($c);
        return $result;
    }
    public function addfeedback($w){
        $result = Db::table("orange_feedback")
                ->insert($w);
        return $result;
    }
    public function signList($pageNum,$listNum,$w){
        $result["list"] = Db::name("sign")
            ->where($w)
            ->page($pageNum,$listNum)
            ->order("ctime desc")
            ->select();
        $result["total"] = Db::name("sign")
            ->where($w)
            ->count("s_id");
        return $result;
    }
    public function  judgeSign($w){
        $result = Db::name("sign")
                ->where($w)
                ->find();
        return $result;
    }
    public function checkphone($userphone){
        $result = $this
                  ->where(["phone"=>$userphone])
                  ->find();
        return $result;
    }

}